package com.xxxx.crm.controller;

import com.xxxx.crm.base.BaseController;
import com.xxxx.crm.base.ResultInfo;
import com.xxxx.crm.enums.DevResult;
import com.xxxx.crm.enums.StateStatus;
import com.xxxx.crm.query.SaleChanceQuery;
import com.xxxx.crm.service.SaleChanceService;
import com.xxxx.crm.utils.*;
import com.xxxx.crm.vo.SaleChance;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * @Author:王亚栋
 * @Date:2022/12/19 16:53
 * @Version:1.0
 */
@Controller
@RequestMapping("sale_chance")
public class SaleChanceController extends BaseController {

    @Autowired
    private SaleChanceService saleChanceService;

    /**
     * 展示到sale_chance页面
     * @return
     */
    @GetMapping("/index")
    public String index(){

        return "sale_chance/sale_chance";
    }

    /**
     * 多条件查询营销机会
     * @param saleChanceQuery
     * @return
     */
    @GetMapping("/querySaleChanceByParams")
    @ResponseBody
    public Map<String,Object> querySaleChanceByParams(HttpServletRequest request,
                                                      SaleChanceQuery saleChanceQuery,
                                                      Integer flag){

        //如果flag不为空，且值为1 -- 表示当前查询的是客户开发计划
        if (flag != null && flag == 1){
            //查询客户开发计划
            //设置分配状态
            saleChanceQuery.setState(StateStatus.STATED.getType());
            //设置指派人(当前登录用户ID)
            Integer userId = LoginUserUtil.releaseUserIdFromCookie(request);

            saleChanceQuery.setAssignMan(userId);

        }

        return saleChanceService.querySaleChanceByParams(saleChanceQuery);

    }

    /**
     * 添加营销机会
     * 参数校验
     *      customerName 非空
     *      linkMan     非空
     *      linkPhone   非空
     * 设置默认参数
     *      createMan   当前登录用户名
     *      assignMan   指派人
     *          如果未设置指派人 --默认
     *          分配状态
     * @param saleChance
     */
    @PostMapping("/add")
    @ResponseBody
    public ResultInfo addSaleChance(SaleChance saleChance, HttpServletRequest request){

        String userName = CookieUtil.getCookieValue(request,"userName");

        saleChance.setCreateMan(userName);

        saleChanceService.addSaleChance(saleChance);

        return success("营销机会数据添加成功！");
    }

    /**
     * 去到营销机会添加或更新页面
     * @return
     */
    @GetMapping("/toSaleChancePage")
    public String toSaleChancePage(HttpServletRequest request,Integer saleChanceId){

        //判断参数saleChance是否为空
        if (saleChanceId != null){
            //通过id查询营销机会
            SaleChance saleChance = saleChanceService.selectByPrimaryKey(saleChanceId);
            //设置到请求域中
            request.setAttribute("saleChance",saleChance);
        }

        return "sale_chance/add_update";
    }


    /**
     * 更新营销机会
     * @param saleChance
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultInfo updateSaleChance(SaleChance saleChance){

        saleChanceService.updateSaleChance(saleChance);

        return success("营销机会更新成功！");
    }

    /**
     * 删除营销机会
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResultInfo deleteSaleChance(Integer[] ids){

        saleChanceService.deleteBatch(ids);

        return success("删除营销机会数据成功！");
    }


    /**
     * 更新营销机会的开发状态
     * @param id
     * @param devResult
     * @return
     */
    public ResultInfo updateSaleChanceDevResult(Integer id,
                                                Integer devResult){

        saleChanceService.updateSaleChanceDevResult(id,devResult);

        return success("营销机会的开发状态更新成功！");

    }
}
